package mobile.TheOldRepublic.web;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;


public class WebViewWrapper extends WebView{
	
	protected WebViewClientWrapper webViewClient = new WebViewClientWrapper();

	public WebViewWrapper(Context context) {
		super(context);
		init(context);
	}
	
	public WebViewWrapper(Context context, AttributeSet attrs){
		super(context, attrs);
		init(context);
	}
	
	public WebViewWrapper(Context context, AttributeSet attrs, int defStyle){
		super(context,attrs,defStyle);
		init(context);
	}
	
	public void init(Context context){
		this.getSettings().setJavaScriptEnabled(true);
		
		if(context instanceof Activity){
			final Activity activity = (Activity)context;
			// Crashes app
			//activity.getWindow().requestFeature(Window.FEATURE_PROGRESS);
			
			this.setWebChromeClient(new WebChromeClient() {
			   public void onProgressChanged(WebView view, int progress) {
			     // Activities and WebViews measure progress with different scales.
			     // The progress meter will automatically disappear when we reach 100%
			     activity.setProgress(progress * 1000);
			   }
			 });
			webViewClient.setContext(context);
			this.setWebViewClient(webViewClient);
		}
	}
	
	/**
	 * Because the default loadUrl does not call shouldOverrideUrlLoading I'm overriding it.
	 */
	@Override
	public void loadUrl(String url){
		boolean override = false;
		override = this.webViewClient.shouldOverrideUrlLoading(this, url);
		
		if(!override)
			super.loadUrl(url);
	}
	
	//Debug
	public String getDebuggingHtml(){
		return this.webViewClient.getLastHtml();
	}
}